clear
set more off
set mat 3500
set mat 10000

*************** Preamble
***************
***************
***************
***************

***Set version
version 9.2

***Directories
global desktop C:\Users\User\Dropbox\Research\Working Papers\Mental Health\Drafts\220910 EDCC Reformatted\AFKNMentalHealthReplication\
global cleandata $desktop\
global output $desktop\

***Output options
global outopts dec(3) label nocons excel nonotes

***Regression options
global controls female urban
global depvarlist deprsum_nomiss deprsum_avg deprme_miss deprme_avg 
global treatdv depr_diag depr_treat depr_medi 
global levelsdv depr_30 depr_30_2 depr_30_3 depr_30_4 depr_30_5 
global symptomsdv depr_feel12 depr_inter12 depr_ener12 depr_feel2w depr_most depr_app depr_slow 

*************** Text Tables
***************
***************
***************
***************

*** 1) Summary Statistics
clear
set more off

cd "$cleandata"
use whs_africa_clean, clear
cd "$output"

keep deprsum_nomiss deprsum_avg deprfactor deprme_miss deprme_avg ///
	depr_30 depr_30_2 depr_30_3 depr_30_4 depr_30_5 ///
	worry_30 worry_30_2 worry_30_3 worry_30_4 worry_30_5 ///
	depr_diag depr_treat depr_medi depr_feel12 depr_inter12 ///
	depr_ener12 depr_feel2w depr_most depr_app depr_slow ///
	tempL1 logtempL1 $controls yearofbirth

order deprsum_nomiss deprsum_avg deprfactor deprme_miss deprme_avg ///
	depr_30 depr_30_2 depr_30_3 depr_30_4 depr_30_5 ///
	worry_30 worry_30_2 worry_30_3 worry_30_4 worry_30_5 ///
	depr_diag depr_treat depr_medi depr_feel12 depr_inter12 ///
	depr_ener12 depr_feel2w depr_most depr_app depr_slow ///
	tempL1 logtempL1 $controls yearofbirth

cap erase table_sumstats.xml
qui outreg2 using table_sumstats, label auto(2) sum(log) eqkeep(mean sd min max N) excel replace
erase table_sumstats.txt

*** 2) Main Result
cd "$cleandata"
use whs_africa_clean, clear
cd "$output"

cap erase table_main.txt
cap erase table_main.xml
foreach var of varlist $depvarlist  {
		*Without Controls
	reghdfe `var' tempL1 rainL1, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_main, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append
		*With Controls
	reghdfe `var' tempL1 rainL1 $controls, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_main, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append

}
cap erase table_main.txt

*** 3) Trends
/*See end of file: this runs slowly*/

*** 4) Treatment
cd "$cleandata"
use whs_africa_clean, clear
cd "$output"

cap erase table_treat.txt
cap erase table_treat.xml
foreach var of varlist $treatdv  {
		*Without Controls
	reghdfe `var' tempL1 rainL1, a(delid yearofbirth) cl(delid)
		su `var' if e(sample)==1
		local lci=_b[tempL1]-1.96*_se[tempL1]
		local uci=_b[tempL1]+1.96*_se[tempL1]
	outreg2 using table_treat, keep(tempL1) addstat(DV Mean, r(mean),LCI,`lci',UCI,`uci') $outopts append
		*With Controls
	reghdfe `var' tempL1 rainL1 $controls, a(delid yearofbirth) cl(delid)
		su `var' if e(sample)==1
		local lci=_b[tempL1]-1.96*_se[tempL1]
		local uci=_b[tempL1]+1.96*_se[tempL1]
	outreg2 using table_treat, keep(tempL1) addstat(DV Mean, r(mean),LCI,`lci',UCI,`uci') $outopts append

}
cap erase table_treat.txt

*With confidence intervals
cd "$cleandata"
use whs_africa_clean, clear
cd "$output"

cap erase table_treat.txt
cap erase table_treat.xml
foreach var of varlist $treatdv  {
		*Without Controls
	reghdfe `var' tempL1 rainL1, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_treat, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append
		*With Controls
	reghdfe `var' tempL1 rainL1 $controls, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_treat, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append

}
cap erase table_treat.txt

*************** Text Figures
***************
***************
***************
***************

*** 5) Map
cd "$cleandata"
use whs_africa_clean, clear
cd "$output"

keep latitude longitude
duplicates drop
sort lat lon
gen row=_n
outsheet using pointsformapping.csv, comma n replace

*************** Appendix Tables
***************
***************
***************
***************

******* Further description of results
*******
*******
*** Levels
cd "$cleandata"
use whs_africa_clean, clear
cd "$output"

cap erase table_levels.txt
cap erase table_levels.xml
foreach var of varlist $levelsdv  {
		*Without Controls
	reghdfe `var' tempL1 rainL1, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_levels, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append
		*With Controls
	reghdfe `var' tempL1 rainL1 $controls, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_levels, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append

}
cap erase table_levels.txt

*** Symptoms
cap erase table_symptoms.txt
cap erase table_symptoms.xml
foreach var of varlist $symptomsdv {
		*Without Controls
	reghdfe `var' tempL1 rainL1, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_symptoms, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append
		*With Controls
	reghdfe `var' tempL1 rainL1 $controls, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_symptoms, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append

}
cap erase table_symptoms.txt

*** Reporting coefficeint on rainfall
cd "$cleandata"
use whs_africa_clean, clear
cd "$output"

replace rainL1=rainL1/1000
cap erase table_rain.txt
cap erase table_rain.xml
foreach var of varlist $depvarlist  {
		*Without Controls
	reghdfe `var' tempL1 rainL1, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_rain, addstat(DV Mean, r(mean)) keep(tempL1 rainL1) $outopts append
		*With Controls
	reghdfe `var' tempL1 rainL1 $controls, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_rain, addstat(DV Mean, r(mean)) keep(tempL1 rainL1) $outopts append

}
cap erase table_rain.txt

***Heterogeneous Effects by Age
cd "$cleandata"
use whs_africa_clean, clear
cd "$output"

su age, det
gen amage=age>r(p50)

cap erase table_byage.txt
cap erase table_byage.xml
preserve
	keep if amage==1
	foreach var of varlist $depvarlist  {
			*Without Controls
		reghdfe `var' tempL1 rainL1 , a(delid yearofbirth) cl(delid)
		su `var' if e(sample)==1
		outreg2 using table_byage, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append ///
			addtext(Sample, Above Median Age)
			*With Controls
		reghdfe `var' tempL1 rainL1 $controls , a(delid yearofbirth) cl(delid)
		su `var' if e(sample)==1
		outreg2 using table_byage, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append ///
			addtext(Sample, Above Median Age)
	}
restore
preserve
	keep if amage==0
	foreach var of varlist $depvarlist  {
			*Without Controls
		reghdfe `var' tempL1 rainL1 , a(delid yearofbirth) cl(delid)
		su `var' if e(sample)==1
		outreg2 using table_byage, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append ///
			addtext(Sample, Below Median Age)
			*With Controls
		reghdfe `var' tempL1 rainL1 $controls , a(delid yearofbirth) cl(delid)
		su `var' if e(sample)==1
		outreg2 using table_byage, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append ///
			addtext(Sample, Below Median Age)
	}
restore
cap erase table_byage.txt

******* Other temperature measures
*******
*******

*** Inverse Distance Weighting
cd "$cleandata"
use whs_africa_clean, clear
cd "$output"

cap erase table_idw.txt
cap erase table_idw.xml
foreach var of varlist $depvarlist  {
		*Without Controls
	reghdfe `var' idwtempL1 rainL1, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_idw, addstat(DV Mean, r(mean)) keep(idwtempL1) $outopts append
		*With Controls
	reghdfe `var' idwtempL1 rainL1 $controls, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_idw, addstat(DV Mean, r(mean)) keep(idwtempL1) $outopts append

}
cap erase table_idw.txt

******* Other sources of bias
*******
*******

*** Leads and lags
cd "$cleandata"
use whs_africa_clean, clear
cd "$output"

cap erase table_lags.txt
cap erase table_lags.xml
foreach var of varlist $depvarlist {
		*Without Controls
	reghdfe `var' tempF2 tempF1 tempF0 tempL1 tempL2 ///
		rainF2 rainF1 rainF0 rainL1 rainL2, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_lags, addstat(DV Mean, r(mean)) keep(tempF2 tempF1 tempF0 tempL1 tempL2) $outopts append
		*With Controls
	reghdfe `var' tempF2 tempF1 tempF0 tempL1 tempL2 ///
		rainF2 rainF1 rainF0 rainL1 rainL2 $controls, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_lags, addstat(DV Mean, r(mean)) keep(tempF2 tempF1 tempF0 tempL1 tempL2) $outopts append

}
cap erase table_lags.txt

*** Additional Controls: Head, Marital, Education, Ethnicity
cd "$cleandata"
use whs_africa_clean, clear
cd "$output"

gen head_wm=head
	recode head_wm (.=2)
gen marital_wm=marital
	recode marital_wm (.=7)
gen education_wm=education
	recode education_wm (.=8)
gen ethnicity_wm=ethnicity
	replace ethnicity_wm="Ethnicity Not Answered" if ethnicity_wm==""
	egen ethnicity_wm_num=group(ethnicity_wm)

cap erase table_morecont.txt
cap erase table_morecont.xml
foreach var of varlist $depvarlist  {
		*Without Controls
	reghdfe `var' tempL1 rainL1, a(delid yearofbirth head_wm marital_wm education_wm ethnicity_wm_num) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_morecont, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append
		*With Controls
	reghdfe `var' tempL1 rainL1 $controls, a(delid yearofbirth head_wm marital_wm education_wm ethnicity_wm_num) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_morecont, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append

}
cap erase table_morecont.txt

*** Control for GDP
cd "$cleandata"
use whs_africa_clean, clear
cd "$output"

cap erase table_gdp.txt
cap erase table_gdp.xml
foreach var of varlist $depvarlist  {
		*Without Controls
	reghdfe `var' tempL1 rainL1 L1lngdppc, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_gdp, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append
		*With Controls
	reghdfe `var' tempL1 rainL1 L1lngdppc $controls, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_gdp, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append

}
cap erase table_gdp.txt

*** Other FE: UN Region X Year 
cd "$cleandata"
use whs_africa_clean, clear
cd "$output"

cap erase table_ryfe.txt
cap erase table_ryfe.xml
egen celyrfe=group(yearofbirth unreg)
foreach var of varlist $depvarlist  {
		*Without Controls
	reghdfe `var' tempL1 rainL1, a(delid celyrfe) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_ryfe, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append
		*With Controls
	reghdfe `var' tempL1 rainL1 $controls, a(delid celyrfe) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_ryfe, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append
}
cap erase table_ryfe.txt

*** Other FE : Ethnicity
cd "$cleandata"
use whs_africa_clean, clear
cd "$output"
replace ethnicity="Missing" if ethnicity==""

cap erase table_ethfe.txt
cap erase table_ethfe.xml
foreach var of varlist $depvarlist  {
		*Without Controls
	reghdfe `var' tempL1 rainL1, a(delid yearofbirth ethnicity) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_ethfe, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append
		*With Controls
	reghdfe `var' tempL1 rainL1 $controls, a(delid yearofbirth ethnicity) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_ethfe, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append

}
cap erase table_ethfe.txt

*** Other Clusters: Grid Cell
cd "$cleandata"
use whs_africa_clean, clear
cd "$output"

cap erase table_cellcluster.txt
cap erase table_cellcluster.xml
forvalues i=1(1)5 {
gen latround=`i'*round(latitude/`i')
gen lonround=`i'*round(latitude/`i')
egen cellid=group(latround lonround)
foreach var of varlist $depvarlist  {
		*Without Controls
	reghdfe `var' tempL1 rainL1, a(delid yearofbirth) cl(cellid)
	su `var' if e(sample)==1
	outreg2 using table_cellcluster, addstat(DV Mean, r(mean)) addtext(Cell Size, `i') keep(tempL1) $outopts aster(se) append
		*With Controls
	reghdfe `var' tempL1 rainL1 $controls, a(delid yearofbirth) cl(cellid)
	su `var' if e(sample)==1
	outreg2 using table_cellcluster, addstat(DV Mean, r(mean)) addtext(Cell Size, `i') keep(tempL1) $outopts aster(se) append
}
drop latround lonround cellid
}
cap erase table_cellcluster.txt

***Remove probable migrants 
cd "$cleandata"
use whs_africa_clean, clear
cd "$output"

cap erase table_migr.txt
cap erase table_migr.xml
foreach var of varlist $depvarlist  {
		*Without Controls
	reghdfe `var' tempL1 rainL1 if shej>=0.1, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_migr, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append
		*With Controls
	reghdfe `var' tempL1 rainL1 $controls if shej>=0.1, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_migr, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append

}
cap erase table_migr.txt

*** Cohort Size
cd "$cleandata"
use whs_africa_clean, clear
cd "$output"

gen one=1
bysort delid yearofbirth  : egen csize=count(one)
	label var csize "Cohort Size"
gen lncsize=ln(csize)
	label var lncsize "ln Cohort Size"
bysort delid yearofbirth : egen modecountry=mode(country), minmode
	replace country=modecountry
order delid yearofbirth csize
keep delid yearofbirth csize lncsize ///
	tempL1 rainL1 ///
	country 
duplicates drop
isid delid yearofbirth
duplicates drop delid yearofbirth, force

cap erase table_csize.txt
cap erase table_csize.xml
foreach var of varlist csize lncsize {
		*Without Controls
	reghdfe `var' tempL1 rainL1, a(delid yearofbirth) cl(delid)
		local lci=_b[tempL1]-1.96*_se[tempL1]
		local uci=_b[tempL1]+1.96*_se[tempL1]
	su `var' if e(sample)==1
	outreg2 using table_csize, keep(tempL1) addstat(DV Mean, r(mean), LCI,`lci',UCI,`uci') $outopts append
}
cap erase table_csize.txt

*** Alternative levels of aggregation
*Variables used
global collapsevars $depvarlist tempL1 rainL1 $controls

*Define program
cap program drop aggregation
program define aggregation
	args iid tid cid
	
	*Load Data
	cd "$cleandata"
	use whs_africa_clean, clear
	cd "$output"

	*Collapse
	egen cellid=group(`iid' `tid')
	foreach var of varlist $collapsevars {
		bysort cellid : egen junk=mean(`var')
		replace `var'=junk
		drop junk
	}
	keep cellid $collapsevars `iid' `tid' `cid'
	duplicates drop
	isid cellid
	
	*Regressions
	cap erase table_`iid'_`tid'_`cid'.txt
	cap erase table_`iid'_`tid'_`cid'.xml
	foreach var of varlist $depvarlist  {
			*Without Controls
		reghdfe `var' tempL1 rainL1, a(`iid' `tid') cl(`cid')
		su `var' if e(sample)==1
		outreg2 using table_`iid'_`tid'_`cid', addstat(DV Mean, r(mean)) keep(tempL1) $outopts append
			*With Controls
		reghdfe `var' tempL1 rainL1 $controls, a(`iid' `tid') cl(`cid')
		su `var' if e(sample)==1
		outreg2 using table_`iid'_`tid'_`cid', addstat(DV Mean, r(mean)) keep(tempL1) $outopts append
	}
	cap erase table_`iid'_`tid'_`cid'.txt
end

*Grid Cell X Year
aggregation delid yearofbirth delid
aggregation country yearofbirth country

*************** Runs Slowly
***************
***************
***************
***************

*** 3) Grid-Cell Trends
/*Note: Runs very slowly*/

cd "$cleandata"
use whs_africa_clean, clear
cd "$output"

xi, pref(trd) i.delid*yearofbirth
drop trddelid_*

cap erase table_trends.txt
cap erase table_trends.xml
foreach var of varlist $depvarlist  {
		*Without Controls
	xi : reghdfe `var' tempL1 rainL1 trd*, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_trends, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append
		*With Controls
	xi : reghdfe `var' tempL1 rainL1 $controls trd*, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_trends, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append
}
cap erase table_trends.txt

*************** Not Reported
***************
***************
***************
***************

*** Schizophrenia
cd "$cleandata"
use whs_africa_clean, clear
cd "$output"

cap erase table_sczsum.txt
cap erase table_sczsum.xml
foreach var of varlist sczsum {
		*Without Controls
	reghdfe `var' tempL1 rainL1, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_sczsum, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append
		*With Controls
	reghdfe `var' tempL1 rainL1 $controls, a(delid yearofbirth) cl(delid)
	su `var' if e(sample)==1
	outreg2 using table_sczsum, addstat(DV Mean, r(mean)) keep(tempL1) $outopts append

}
cap erase table_sczsum.txt

